"""A module that deletes all UnicodeChar records in the datastore. Works on both
local and remote datastores.
"""

from google.appengine.ext import db
import optparse
import os
import tempfile

from ui import models
from scripts import remote

def delete_all_remote():
  batch = remote.DatastoreReadBatchProcess(models.UnicodeChar, db.delete,
                                           "code",
                                           servername=remote.SERVER_REMOTE,
                                           description="Delete all UnicodeChar \
                                             entities")
  batch.execute()

def delete_all_local():
  path = os.path.join(tempfile.gettempdir(), "dev_appserver.datastore")
  if os.path.lexists(path):
    try:
      os.remove(path)
      print("Removed datastore at %s. Restart dev_appserver if running." % path)
    except OSError: 
      print("Failed to remove datastore at %s. Is it in use?" % path)  
  else:
    print("Datastore not found at %s." % path)

def main():
  parser = optparse.OptionParser()
  parser.add_option("-r", "--remote", default=False, action="store_true",
                    help="Run this script on the remote instance of this app")
  (options, args) = parser.parse_args()
  args # silence warning "args is never used"
  
  if options.remote:
    delete_all_remote()
  else:
    delete_all_local()
  
if __name__ == "__main__":
  main()
